class skylineAnalysis {
  constructor(viewer) {
    this.viewer = viewer
    this.skyline = undefined
    this.hasSkyLineAnalysisResult = false
  }

  remove() {
    for(let layer of this.viewer.scene.layers.layerQueue){
      layer.removeAllObjsColor();
    }
    if(this.skyline){
      this.skyline.destroy();
      this.skyline = undefined;
    }
    this.hasSkyLineAnalysisResult = false;
  }

  analysis(formSkyline){
    let scene = this.viewer.scene;
    if(!this.skyline){
      this.skyline = new Cesium.Skyline(scene);
    }
    this.clear(formSkyline);
    let cartographic = scene.camera.positionCartographic;
    let longitude = Cesium.Math.toDegrees(cartographic.longitude);
    let latitude = Cesium.Math.toDegrees(cartographic.latitude);
    let height = cartographic.height;
    // 天际线分析的视口位置设置成当前相机位置
    this.skyline.viewPosition = [longitude, latitude, height];
    formSkyline.observerInformation=longitude.toFixed(4) + ', ' + latitude.toFixed(4) + ', ' + height.toFixed(2)
    //设置俯仰和方向
    this.skyline.pitch = Cesium.Math.toDegrees(scene.camera.pitch);
    this.skyline.direction = Cesium.Math.toDegrees(scene.camera.heading);
    this.skyline.radius = formSkyline.skylineRadius;
    this.skyline.build();
    this.hasSkyLineAnalysisResult = true; // 表示有了分析结果，可以提取二维天际线和高亮障碍物
    this.skyline.color=Cesium.Color.fromCssColorString(formSkyline.skylineColor);
  }

  highlightBarrier(value){
    if(this.skyline && this.hasSkyLineAnalysisResult){
      let skylineHighlightBarrierColor = Cesium.Color.fromCssColorString(value);
      for(let index in this.skyline.getObjectIds()){
        var layer = this.viewer.scene.layers.findByIndex(index - 3); // 底层索引从3开始
        layer.setObjsColor(skyline.getObjectIds()[index], skylineHighlightBarrierColor);
      }
    }
  };

  clear(formSkyline){
    let scene = this.viewer.scene;
    formSkyline.observerInformation=''
    this.viewer.entities.removeAll();
    scene.primitives._primitives = [];
    this.skyline.clear();
    for(let layer of scene.layers.layerQueue){
      layer.removeAllObjsColor();
    }
    this.hasSkyLineAnalysisResult = false;
  }

  changeSkylineColor(value){
    if(this.skyline){
      this.skyline.color=Cesium.Color.fromCssColorString(value);
    }
  }

  changeSkylineMode(value){
    if(value==="polyline"){
      if(this.skyline){
        this.skyline.displayStyle = 0;
        this.viewer.scene.primitives._primitives = [];
      }
    } else if(value==="polygon"){
      if(this.skyline){
        this.skyline.displayStyle = 1;
        this.viewer.scene.primitives._primitives = [];
      }
    }
  }
}

export {skylineAnalysis}

